#include "dominion.h"
#include "dominion_helpers.h"
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include "rngs.h"

#define DEBUG 0
#define NOISY_TEST 1

int main () {
    int k[10] = {adventurer, council_room, feast, gardens, mine,
	       remodel, smithy, village, baron, great_hall};
    int player1 = 0;
    int handCount = 5;
    int pccount, hcount, card, lcard, i;
    struct gameState G;

    printf ("Testing discardCard.\n");

    i=2; //this is the index in the handarray of the card to be discarded
#if (NOISY_TEST == 1)
    printf("Testing discardCard: no trash card, position %d.\n", i+1);
#endif

    initializeGame(2, k, 1, &G);
    G.handCount[player1] = handCount;
    memcpy(G.hand[player1], k, sizeof(int) * handCount); // set hand to [adventurer, council_room, feast, gardens, mine]   

    //save info from current gamestate
    hcount = G.handCount[player1];
    pccount = G.playedCardCount;
    lcard = G.hand[player1][G.handCount[player1]-1];
    card = G.hand[player1][i];

    discardCard(i, player1, &G, 0);
    
    assert(G.handCount[player1] == hcount-1);
    assert(G.playedCardCount == pccount+1);
    assert(G.playedCards[G.playedCardCount-1]==card);
    assert(G.hand[player1][i] == lcard);

#if (NOISY_TEST == 1)
    printf("Test passed.\n");
#endif

    i=handCount-1; //this is the index in the handarray of the card to be discarded
#if (NOISY_TEST == 1)
    printf("Testing discardCard: no trash card, position %d. (end of array)\n", i+1);
#endif

    initializeGame(2, k, 1, &G);
    G.handCount[player1] = handCount;
    memcpy(G.hand[player1], k, sizeof(int) * handCount); // set hand to [adventurer, council_room, feast, gardens, mine]   

    //save info from current gamestate
    hcount = G.handCount[player1];
    pccount = G.playedCardCount;
    card = G.hand[player1][i];

    discardCard(i, player1, &G, 0);
    
    assert(G.handCount[player1] == hcount-1);
    assert(G.playedCardCount == pccount+1);
    assert(G.playedCards[G.playedCardCount-1]==card);

#if (NOISY_TEST == 1)
    printf("Test passed.\n");
#endif

    i=0; //this is the index in the handarray of the card to be discarded
#if (NOISY_TEST == 1)
    printf("Testing discardCard: no trash card, position %d. (only card in array)\n", i+1);
#endif

    initializeGame(2, k, 1, &G);
    G.handCount[player1] = handCount-handCount+1;
    memcpy(G.hand[player1], k, sizeof(int) * handCount); // set hand to [adventurer]   

    //save info from current gamestate
    hcount = G.handCount[player1];
    pccount = G.playedCardCount;
    card = G.hand[player1][i];

    discardCard(i, player1, &G, 0);
    
    assert(G.handCount[player1] == hcount-1);
    assert(G.playedCardCount == pccount+1);
    assert(G.playedCards[G.playedCardCount-1]==card);

#if (NOISY_TEST == 1)
    printf("Test passed.\n");
#endif

    i=2; //this is the index in the handarray of the card to be discarded
#if (NOISY_TEST == 1)
    printf("Testing discardCard: trash card, position %d.\n", i+1);
#endif

    initializeGame(2, k, 1, &G);
    G.handCount[player1] = handCount;
    memcpy(G.hand[player1], k, sizeof(int) * handCount); // set hand to [adventurer, council_room, feast, gardens, mine]   

    //save info from current gamestate
    hcount = G.handCount[player1];
    lcard = G.hand[player1][G.handCount[player1]-1];

    discardCard(i, player1, &G, 1);
    
    assert(G.handCount[player1] == hcount-1);
    assert(G.hand[player1][i] == lcard);

#if (NOISY_TEST == 1)
    printf("Test passed.\n");
#endif

    i=handCount-1; //this is the index in the handarray of the card to be discarded
#if (NOISY_TEST == 1)
    printf("Testing discardCard: no trash card, position %d. (end of array)\n", i+1);
#endif

    initializeGame(2, k, 1, &G);
    G.handCount[player1] = handCount;
    memcpy(G.hand[player1], k, sizeof(int) * handCount); // set hand to [adventurer, council_room, feast, gardens, mine]   

    //save info from current gamestate
    hcount = G.handCount[player1];
    card = G.hand[player1][i];

    discardCard(i, player1, &G, 1);
    
    assert(G.handCount[player1] == hcount-1);

#if (NOISY_TEST == 1)
    printf("Test passed.\n");
#endif

    i=0; //this is the index in the handarray of the card to be discarded
#if (NOISY_TEST == 1)
    printf("Testing discardCard: no trash card, position %d. (only card in array)\n", i+1);
#endif

    initializeGame(2, k, 1, &G);
    G.handCount[player1] = handCount-handCount+1;
    memcpy(G.hand[player1], k, sizeof(int) * handCount); // set hand to [adventurer]   

    //save info from current gamestate
    hcount = G.handCount[player1];
    card = G.hand[player1][i];

    discardCard(i, player1, &G, 1);
    
    assert(G.handCount[player1] == hcount-1);

#if (NOISY_TEST == 1)
    printf("Test passed.\n");
#endif

    printf("discardCard(): All tests passed!\n");

    return 0;
}
